package entry

import (
	"fmt"
	"log"

	"github.com/anhoder/foxful-cli/util"
	netease_util "github.com/go-musicfox/netease-music/util"
	"github.com/gookit/gcli/v2"

	"gitee.com/nidielaila/go-mf/internal/commands"
	"gitee.com/nidielaila/go-mf/internal/configs"
	"gitee.com/nidielaila/go-mf/internal/types"
	"gitee.com/nidielaila/go-mf/utils"
)

func runCLI() {
	log.SetOutput(utils.LogWriter())

	var app = gcli.NewApp()
	app.Name = types.AppName
	
	app.Version = types.AppVersion
	if types.BuildTags != "" {
		app.Version += " [" + types.BuildTags + "]"
	}

	app.Description = types.AppDescription
	app.GOptsBinder = func(gf *gcli.Flags) {
		gf.BoolOpt(&commands.GlobalOptions.PProfMode, "pprof", "p", false, "enable PProf mode")
	}
	// 加载go-mf config 文件 ~/.config/go-mf/go-mf.ini
	utils.LoadIniConfig()

	utils.PrimaryColor = configs.ConfigRegistry.Main.PrimaryColor

	var (
		logo = util.GetAlphaAscii(app.Name)
		randomColor = util.GetPrimaryColor()
		logoColorful = util.SetFgStyle(logo, randomColor)
	)

	gcli.AppHelpTemplate = fmt.Sprintf(types.AppHelpTemplate, logoColorful)
	app.Logo.Text = logoColorful

	// update netease config
	netease_util.UNMSwitch = configs.ConfigRegistry.UNM.Enable
	netease_util.Sources = configs.ConfigRegistry.UNM.Sources
	netease_util.SearchLimit = configs.ConfigRegistry.UNM.SearchLimit
	netease_util.EnableLocalVip = configs.ConfigRegistry.UNM.EnableLocalVip
	netease_util.UnlockSoundEffects = configs.ConfigRegistry.UNM.UnlockSoundEffects

	var playerCommand = commands.NewPlayerCommand()
	app.Add(playerCommand)
	app.Add(commands.NewConfigCommand())

	app.DefaultCommand(playerCommand.Name)

	app.Run()
}